package com.jiang.jetpackdemo.utils

import android.util.Log
import com.jiang.jetpackdemo.model.NewsInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements
import java.lang.Exception

/**
 *
 * @ProjectName:    JetpackDemo
 * @ClassName:      WebCrawler
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/4/6 14:55
 */
class WebCrawler {
    companion object {
        private const val TAG = "WebCrawler"
        private const val USER_AGENT_KEY = "user-agent"
        private const val USER_AGENT_VALUE =
            "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
        private const val BASE_URL = "https://www.leiphone.com/category/ai"
        private fun getWebDocument(url: String): Document {
            return Jsoup.connect(url).header(USER_AGENT_KEY, USER_AGENT_VALUE).get()
        }

        suspend fun getNewsInfo(): MutableList<NewsInfo> {
            return withContext(Dispatchers.IO) {
                val list: MutableList<NewsInfo> = ArrayList()
                val titleList: MutableList<String> = ArrayList()
                val imgList: MutableList<String> = ArrayList()
                val desList: MutableList<String> = ArrayList()
                val urlList: MutableList<String> = ArrayList()
                try {
                    val document = getWebDocument(BASE_URL)

                    document.select("img").forEach {
                        if (it.attr("class") == "lazy") {
                            titleList.add(it.attr("title"))
                            imgList.add(it.attr("data-original"))
                        }
                    }

                    document.select("div").forEach {
                        if (it.attr("class") == "des") desList.add(it.text())
                    }

                    document.select("a").forEach {
                        if (it.attr("class") == "headTit") urlList.add(it.attr("href"))
                    }

                    titleList.forEachIndexed { i, e ->
                        list.add(NewsInfo((i + 1).toLong(), e, urlList[i], desList[i], imgList[i]))
                    }

                } catch (e: Exception) {
                    Log.e(TAG, "${e.message}", e)
                }
                return@withContext list
            }
        }

        suspend fun getNewsContent(url: String) {
            return withContext(Dispatchers.IO) {
                val webDocument = getWebDocument(url)
                val allP = webDocument.select("p")

                allP.forEach {
                    Log.d(TAG, "${it.text()}::${it.attr("style")}")
                }
            }
        }

    }
}